<?php
declare (strict_types=1);

namespace app\course\model\exam\admin;

use app\common\model\BaseModel;
use app\exam\model\admin\Paper;
use app\course\logic\base\CourseCert;

/**
 * Class CourseExamLink
 *
 * @package app\course\model\exam\admin
 */
class CourseExamLink extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'course_exam_link';

    /**
     * 自动写入时间
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 试卷名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/28
     *
     * @return \think\model\relation\BelongsTo
     */
    public function paperTitle()
    {
        return $this
            ->belongsTo(Paper::class, 'paper_id', 'id')
            ->bind(['paper_title' => 'title'])
            ->removeOption('soft_delete')
            ->removeWhereField('mhm_id');
    }

    /**
     * 成功添加之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2021-10-13
     */
    public static function onAfterInsert($data)
    {
        // 检查是否发放证书
        app(CourseCert::class)->addCert($data->course_id);
    }

    /**
     * 成功删除之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2021-10-13
     */
    public static function onAfterDelete($data)
    {
        // 检查是否发放证书
        app(CourseCert::class)->addCert($data->course_id);
    }

}